<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: ind.class.bezugstoffe.php 506 2014-01-31 16:44:29Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 506 $
* 	zuletzt geaendert von:	$Author: $
* 	Datum:					$Date: 2014-01-31 16:44:29 +0000 (Fri, 31 Jan 2014) $
*
* 	extends by dirk.mueller (edv-mueller.com)
* 	http://www.itdotmedia.de
*
* --------------------------------------------------------------------------------------
* 	based on:
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
* 	Copyright (c) since 2011 SEO:mercari
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */
include_once (DIR_WS_INCLUDES.'modules/product_konfigurator/class.gallery.php');

if(!defined('TABLE_KONFIGURATOR_OPTIONS_DETAILS_BEZUGSTOFFE'))
  define('TABLE_KONFIGURATOR_OPTIONS_DETAILS_BEZUGSTOFFE', 'mercari_konfigurator_options_details_bezugstoffe');

class bezugstoffe extends gallery {

  function set_group($group) {
    $this->group = $group;
    $this->group['type'] = 'gallery';
  }
  
  function print_js_base($product_id, $with_form_action) {
  	$d = $this->group['type'];
    $this->group['type'] = 'bezugstoffe';
  	parent::print_js_base($product_id, $with_form_action);
    $this->group['type'] = $d;
  	return;
  }

  function assign_template_bezugstoffe(&$smarty, $product_id, $sql, $with_form_action) {
    global $db, $product;

    $option_smarty = new Smarty();
    
    if (!$this->assign_template_base($option_smarty, $product_id)) return;
    $template_name = $option_smarty->getTemplateVars('TEMPLATE');
    
    $option_smarty->assign('FORM_ACTION', $with_form_action);
		if (isset($_GET['p']) && ($p = encode_urlsearch($_GET['p'])) != '') {
			$parameter = parse_product_options($p);
			if(isset($parameter[$this->group['id']]))
				$option_smarty->assign('LOAD_VALUE', $parameter[$this->group['id']]);
		}

    $query = $db->db_query($sql, true);
    $options = array('FRONT' => array(), 'REAR' => array());
		if($query->_numOfRows > 0){
			while(!$query->EOF){
        if (substr($query->fields['konfigurator_option'], 0, 1) == 'F')
          $option =& $options['FRONT'];
        else
          $option =& $options['REAR'];

        $bezug_id = substr($query->fields['konfigurator_option'], 2);
        $color_id = $query->fields['bezug_id'];
        $price = number_format($query->fields['price'], 2, '.', '');

        if (!isset($option['price_'.$price])) $option['price_'.$price]
          = array(
            'value' => $query->fields['price'],
            'options' => array()
          );
        if (!isset($option['price_'.$price]['options'][$bezug_id]))
          $option['price_'.$price]['options'][$bezug_id]
            = array(
                'title' => $query->fields['konfigurator_options_name'],
                'options' => $this->parse_parameters($query->fields['konfigurator_options_parameter']),
                'colors' => array()
              );

        $option['price_'.$price]['options'][$bezug_id]['colors'][$query->fields['bezug_id']]
          = array(
            'title' => $query->fields['bezug_name'],
            'img'   => $query->fields['bezug_img']
          );

        $query->moveNext();
      }
    }

    $option_smarty->assign('OPTIONS_FRONT', $options['FRONT']);
    $option_smarty->assign('OPTIONS_REAR', $options['REAR']);
    $option_smarty->assign('IMAGE_MAIN', DIR_WS_INFO_IMAGES.$product->data['products_image']);
    $option_smarty->assign('TEMPLATE_FILE', $smarty->getTemplateVars('TEMPLATE_FILE'));

  	$option_smarty->assign('language', $_SESSION['language']);
  	if (!CacheCheck()) {
      $option_smarty->caching = false;
      $option_box = $option_smarty->fetch(CURRENT_TEMPLATE.'/module/product_konfigurator/'.$template_name);
      $option_popup = $option_smarty->fetch(CURRENT_TEMPLATE.'/module/product_konfigurator/gallery.'.strtolower($this->group['id']).'.html');
    } else {
      $option_smarty->caching = true;
      $option_smarty->cache_lifetime = CACHE_LIFETIME;
      $option_smarty->cache_modified_check = CACHE_CHECK;
      $cache_id_box = $product->data['products_id'].$this->group['id'].'box'.$_SESSION['language'].$_SESSION['customers_status']['customers_status_name'];
  		$option_box = $option_smarty->fetch(CURRENT_TEMPLATE.'/module/product_konfigurator/'.$template_name, $cache_id_box);
      $cache_id_popup = $product->data['products_id'].$this->group['id'].'popup'.$_SESSION['language'].$_SESSION['customers_status']['customers_status_name'];
  		$option_popup = $option_smarty->fetch(CURRENT_TEMPLATE.'/module/product_konfigurator/gallery.'.strtolower($this->group['id']).'.html', $cache_id_box);
    }
    
    $MODULE_product_options = $smarty->getTemplateVars('MODULE_product_options');
    if ($MODULE_product_options != '') $MODULE_product_options .= "\n";
    $smarty->assign('MODULE_product_options', $MODULE_product_options . $option_box);
    
    $LAYER_KONFIGURATOR = $smarty->getTemplateVars('LAYER_KONFIGURATOR');
    if ($LAYER_KONFIGURATOR != '') $LAYER_KONFIGURATOR .= "\n";
    $smarty->assign('LAYER_KONFIGURATOR', $LAYER_KONFIGURATOR . $option_popup);
  }

  function get_price_bezugstoffe($sql, $price) {
    global $db;
    $price_value = 0;

    $query = $db->db_query($sql, true);
    
    $price_value = 0;
		if($query->_numOfRows > 0){
      while(!$query->EOF) {
        $price_value += (($_p = $query->fields['price']) == null ? 0 : $_p);
        $query->moveNext();
      }
    }

    if (KONFIGURATOR_PRICE_IS_BRUTTO === 'true') {
      $price['brutto'] = $price_value;
      $price['netto'] = $price_value / (1.0 + $price['tax']['rate']/100.0);
    }
    else {
      $price['netto'] = $price_value;
      $price['brutto'] = $price_value * (1.0 + $price['tax']['rate']/100.0);
    }
    $price['tax']['value'] = $price['brutto'] - $price['netto'];

    return $price;
  }

  function get_description_bezugstoffe($sql, $description) {
    global $db;
    $price_value = 0;

    $query = $db->db_query($sql, true);

		if($query->_numOfRows > 0){
      while(!$query->EOF) {
        $description['option_name'] = $query->fields['konfigurator_optiongroups_name'];
        $description['option_value'][]
          = ( $query->_numOfRows == 1 ? '' : ( (substr($query->fields['konfigurator_option'], 0, 1) == 'F') ? 'Sitz: ' : 'Lehne: ' ) )
          . $query->fields['konfigurator_options_name'] . ' ' . $query->fields['bezug_name'];
        $query->moveNext();
      }
    }
    $description['option_value'] = implode('; ', $description['option_value']);
    return $description;
  }
  

}